package com.jiyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiyun.entity.Qualification;
import com.jiyun.entity.User;
import com.jiyun.service.IQualificationService;
import com.jiyun.service.IUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 *  控制层
 * </p>
 *
 * @author 聪哥哥
 * @since 2023-02-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @DubboReference
    IUserService userService;

    /**
     * 添加接口
     */
    @RequestMapping("/add")
    public void addUser(@RequestBody User user){
        userService.save(user);
    }

    /**
     * 修改接口
     */
    @RequestMapping("/update")
    public void updateUser(@RequestBody User user){
        userService.updateById(user);
    }

    /**
     * 单个删除接口
     */
    @RequestMapping("/deleteOne")
    public void deleteOne(@RequestParam Integer id){
        userService.removeById(id);
    }

    /**
     * 批量删除接口
     * 前台ajax参数ids传入示例：1,2,3  (英文逗号分隔的字符串)
     */
    @RequestMapping("/deleteBatch")
    public void deleteBatch(@RequestParam List<Integer> ids){
        userService.removeByIds(ids);
    }

    /**
     * 根据id查询实体接口
     */
    @RequestMapping("/findById")
    public User findById(@RequestParam Integer id){
        return userService.getById(id);
    }

    /**
     * 查询所有接口
     */
    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.list();
    }

    /**
     * 分页查询接口
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     * 重要提示：启用分页功能必须在配置类中添加mybatis-plus分页的拦截器
     */
    @RequestMapping("/findPage")
    public Page<User> findPage(@RequestParam Integer page,@RequestParam Integer pageSize){
        Page<User> userPage = new Page<>(page, pageSize);
        return userService.page(userPage);
    }

    @RequestMapping("login")
    public Object login(@RequestBody User user){
        String result = "";

        User one = userService.findUserByUsername(user.getUsername());

        if (one==null) {
            result = "用户不存在";
            return result;
        }
        if (!one.getPassword().equals(user.getPassword())) {
            result = "密码不对";
            return result;
        }
        //登录之后的业务逻辑，后续再写

        //在存入抽奖资格之前，需要校验是否已存在资格
        //查询当前人、当前时间、活动id为1的活动是否存在资格


        Qualification q = qualificationService.findHasQualification(user.getId(),LocalDate.now(),1);
        if(q==null){//如果返回null，证明当前抽奖活动，今日的资格没有
            //存入抽奖资格
            Qualification qualification = new Qualification();
            qualification.setUid(one.getId());//登录人
            qualification.setQdate(LocalDate.now());//登录时间
            qualification.setHid(1);
            qualification.setState("未使用");//当前抽奖资格是否使用
            qualificationService.save(qualification);
        }



        return one;

    }

    @DubboReference
    IQualificationService qualificationService;

    @RequestMapping("checkUser")
    public Qualification checkUser(Integer uid,Integer hid){
        return qualificationService.findHasQualification(uid,LocalDate.now(),hid);
    }

}
